
const router = require('koa-router')();
const DB = require('../module/db.js');
const url = require('url');


router.use(async (ctx, next) => {
	
	let pathname = url.parse(ctx.request.url).pathname;
	
	// 查询导航数据
	let navResult = await DB.findOne('nav', {
		status: 1
	}, {
		sort: { sort: 1 }
	});

	// 配置seo
	let setting = await DB.findOne('setting', {});
	
	ctx.state.nav = navResult;
	ctx.state.pathname = pathname;
	ctx.state.setting = setting[0];

	
	await next();
});


router.get('/', async (ctx) => {

	let bannerResult = await DB.findOne('focus', {
		status: 1
	}, {
		sort: { sort: 1 }
	});

	// 获取友情链接
	let links = await DB.findOne('link',{})
	ctx.render('default/index', {
		focus: bannerResult,
		links: links
	});
});


router.get('/news', async (ctx) => {
	// 获取分类
	// 获取url参数cid
	let cid = ctx.query.cid;
	let articleResult = [];
	let articleCount = 0;
	let pageindex = (ctx.query.page) ? parseInt(ctx.query.page, 0) : 1;
	let pagesize = (ctx.query.size) ? parseInt(ctx.query.size, 0) : 10;

	// 获取二级分类
	let cateResult = await DB.findOne('articlecate', {
		pid: '5b28acf858c2c728a88cbe9f'
	});

	if(cid){
		// 查询总数量
		articleCount = await DB.count('article', {
			pid: cid
		});

		// 根据cid条件查
		articleResult = await DB.findOne('article', {
			pid: cid
		}, {
			index: pageindex,
			size: pagesize
		});
	}else {
		// 循环获取子分类的内容
		let subCateArr = [];
		for(let i = 0; i < cateResult.length; i++){
			subCateArr.push(cateResult[i]._id.toString());
		}

		// 查询总数量
		articleCount = await DB.count('article', {
			pid: {
				$in: subCateArr
			}
		});

		articleResult = await DB.findOne('article', {
			pid: {
				$in: subCateArr
			}
		}, {
			index: pageindex,
			size: pagesize
		});
	}

	ctx.render('default/news', {
		cateList: cateResult,
		articleList: articleResult,
		cid: cid,
		totalPages: Math.ceil(articleCount / pagesize),
		pageIndex: pageindex
	});
});


router.get('/service', async (ctx) => {

	let cateResult = await DB.findOne('articlecate', {
		pid: '5b28acf058c2c728a88cbe9e'
	});

	// 循环获取子分类的内容
	let subCateArr = [];
	for(let i = 0; i < cateResult.length; i++){
		subCateArr.push(cateResult[i]._id.toString());
	}
	let articleResult = await DB.findOne('article', {
		pid: {
			$in: subCateArr
		}
	});

    ctx.render('default/service', {
		cateList: cateResult,
    	list: articleResult
    });
});


router.get('/content/:id', async (ctx) => {
	
	// ctx.params.id;
	
	let id = ctx.params.id;
	
	let contentResult = await DB.findOne('article', {
		_id: DB.getObjectID(id)
	});

	// 获取当前文章的分类信息
	let cateResult = await DB.findOne('articlecate', {
		_id: DB.getObjectID(contentResult[0].pid)
	});

	let navResult = '';
	if(!cateResult[0].pid || cateResult[0].pid == '0'){
		// 导航标查找当前分类对应的url链接地址
		navResult = await DB.findOne('nav', {
			title: cateResult[0].title
		});
	}else {
		let parentCateResult = await DB.findOne('articlecate', {
			_id: DB.getObjectID(cateResult[0].pid)
		});
		navResult = await DB.findOne('nav', {
			$or: [{
				title: cateResult[0].title
			}, {
				title: parentCateResult[0].title
			}]
		});
	}

	if(navResult && navResult.length > 0) {
		// 将导航url赋值给pathname
		ctx.state.pathname = navResult[0].url;
	}

    ctx.render('default/content', {
    	list: contentResult[0]
    });
});


router.get('/about', async (ctx) => {

    ctx.render('default/about');
});


router.get('/case', async (ctx) => {

	// 获取url参数cid
	let cid = ctx.query.cid;
	let articleResult = [];
	let articleCount = 0;
	let pageindex = (ctx.query.page) ? parseInt(ctx.query.page, 0) : 1;
	let pagesize = (ctx.query.size) ? parseInt(ctx.query.size, 0) : 10;

	// 获取二级分类
	let cateResult = await DB.findOne('articlecate', {
		pid: '5b28ac9a58c2c728a88cbe99'
	});

	if(cid){
		// 查询总数量
		articleCount = await DB.count('article', {
			pid: cid
		});

		// 根据cid条件查
		articleResult = await DB.findOne('article', {
			pid: cid
		}, {
			index: pageindex,
			size: pagesize
		});
	}else {
		// 循环获取子分类的内容
		let subCateArr = [];
		for(let i = 0; i < cateResult.length; i++){
			subCateArr.push(cateResult[i]._id.toString());
		}

		// 查询总数量
		articleCount = await DB.count('article', {
			pid: {
				$in: subCateArr
			}
		});

		articleResult = await DB.findOne('article', {
			pid: {
				$in: subCateArr
			}
		}, {
			index: pageindex,
			size: pagesize
		});
	}

    ctx.render('default/case', {
    	cateList: cateResult,
    	articleList: articleResult,
		cid: cid,
		totalPages: Math.ceil(articleCount / pagesize),
		pageIndex: pageindex
    });
});


router.get('/connect', async (ctx) => {

    ctx.render('default/connect');
});




module.exports = router.routes();